/**
*	@author Ataul Haque
*	@version .02 after CRUD
*	@last_modified 02/03/08
*	
*/
import java.io.*;
import java.util.*;
	
public class BACodeGenerator
{
	private byte[] bdata;
	String table;
	String field[];
	String version=".02";
	
	
	public void writeFile(String FileName,String data)
	{
		try{
			FileOutputStream fout = new FileOutputStream (FileName,true);		    
			//fout.write(bdata);
			new PrintStream(fout).println(data);
			//new PrintStream(fout).println();
		    fout.close();		
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to write to file");
			System.exit(-1);
		}
	}
	/******************************************************** Class*******************************************************/
	public void Class(String FileName)
	{
		String b=new String("");
		b="public class ";
		b+=table;
		writeFile(FileName,b);
		
		b="{";
		writeFile(FileName,b);		
		b="";
		writeFile(FileName,b);
		
		for(int i=0;i<field.length;i++)
		{
			b="	private String ";
			b+=field[i];
			b+=";";
			writeFile(FileName,b);
		}
		//default constractor
		b="	public ";
		b+=table;
		b+="(){}";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		
		//Full constractor
		b="	public ";
		b+=table;
		b+="(";
		for(int i=0;i<field.length;i++)	{
			b+="String ";
			b+=field[i].toLowerCase();
			if(i!=field.length-1)
				b+=",";
		}
		b+="){";
		writeFile(FileName,b);
		for(int i=0;i<field.length;i++)	{
			b="		";
			b+=field[i];
			b+=" = ";
			b+=field[i].toLowerCase();
			b+=";";
			writeFile(FileName,b);
		}
		b="	}";
		writeFile(FileName,b);
		
		for(int i=0;i<field.length;i++)
		{
			b="	public void set";
			b+=field[i];
			b+="(String ";
			b+=field[i].toLowerCase();
			b+="){";
			writeFile(FileName,b);
			b="		";
			b+=field[i];
			b+=" = ";
			b+=field[i].toLowerCase();
			b+=";";
			writeFile(FileName,b);
			b="	}";	
			writeFile(FileName,b);
			b="";
			writeFile(FileName,b);
			
			b="	public String get";
			b+=field[i];
			b+="(){";
			writeFile(FileName,b);
			b="		return ";
			b+=field[i];
			b+=";";			
			writeFile(FileName,b);
			b="	}";		
			writeFile(FileName,b);
			b="";
			writeFile(FileName,b);
			b="";
			writeFile(FileName,b);
		}
		b="}";
		writeFile(FileName,b);
	}
	
	/******************************************************* HTML form ********************************************************/
	
	public void HtmlForm(String FileName)
	{
		String b=new String("");
		b="<form method=\"post\" action=\"add_";
		b+=table;
		b+="_submit.php\">";
		writeFile(FileName,b);
		b="<table>";
		writeFile(FileName,b);
		for(int i=0;i<field.length;i++)
		{
			b="	<tr>";
			writeFile(FileName,b);
			b="		<td>";			
			b+=field[i];
			b+="</td>";
			writeFile(FileName,b);
			
			b="		<td><input type=\"text\" name=\"";			
			b+=field[i];
			b+="\"></td>";
			writeFile(FileName,b);
			
			b="	</tr>";
			writeFile(FileName,b);
		}
		b="	<tr>";
		writeFile(FileName,b);
		b="		<td>";			
		b+="&nbsp";
		b+="</td>";
		writeFile(FileName,b);
		b="		<td><input type=\"submit\"\\></td>";			
		writeFile(FileName,b);
		b="	</tr>";
		writeFile(FileName,b);	
		b="</table>";
		writeFile(FileName,b);
		b="</form>";
		writeFile(FileName,b);
	}
	
		public void SQL(String FileName){
		String b=new String("");
		b="CREATE TABLE ";
		b+=table;
		writeFile(FileName,b);		
		b=" ( ";
		b+=table;
		b+="_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,";
		writeFile(FileName,b);
		for(int i=0;i<field.length;i++){
			b=field[i];
			b+=" VARCHAR( 20 ) NOT NULL";
			if(i!=field.length-1)
				b+=",";
			writeFile(FileName,b);			
		}
		b=");";
		writeFile(FileName,b);		
	}

	
	/************************************************ php file to add ***************************************/
	
	public void addPHP(String FileName)
	{
		String b=new String("");
		b="<?";
		writeFile(FileName,b);
		
		b="require('../init.php');";
		writeFile(FileName,b);
		
		for(int i=0;i<field.length;i++)
		{
			b="$";
			b+=field[i];
			b+=" = addslashes($_POST['";
			b+=field[i];
			b+="']);";
			writeFile(FileName,b);
		}
		
		b="			$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="INSERT INTO ";
		b+=table;
		writeFile(FileName,b);	
		b="(";
		for(int i=0;i<field.length;i++)
		{			
			b+=field[i];
			if(i==field.length-1)
				b+=" ";
			else
				b+=" , ";
		}
		b+=")";	
		writeFile(FileName,b);		
		b="VALUES(";		
		for(int i=0;i<field.length;i++)
		{
			b+="'$";
			b+=field[i];
			if(i==field.length-1)
				b+="' ";
			else
				b+="' , ";
		}
		b+=")";	
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);	
		b="	mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);			
		b= " echo \"Successfully added new ";
		b+=table;
		b+="\";";
		writeFile(FileName,b);		
		b="?>";
		writeFile(FileName,b);	
	}
	
	
	
	/************************************************** get data in php ****************************************/
	
	
	public void getPHP(String FileName)
	{
		String b=new String("");
		b="<?";
		writeFile(FileName,b);
		
		b="require('init.php');";
		writeFile(FileName,b);
		b="	$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SELECT * FROM ";
		b+=table;
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="	mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);					
		b="$num=mysql_numrows($result);";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="for($i=0;$i<$num;$i++)";
		writeFile(FileName,b);
		b="{";
		writeFile(FileName,b);
		for(int i=0;i<field.length;i++)
		{
			b="	$";
			b+=field[i];
			b+=" = mysql_result($result,$i,\"";
			b+=field[i];
			b+="\");";
			writeFile(FileName,b);
		}
		
		b="}";
		writeFile(FileName,b);
		b="?>";
		writeFile(FileName,b);
	}

/***************************************** get php 2 ***********************************/
public void getPHP2(String FileName)
	{
		String b=new String("");
		b="<?";
		writeFile(FileName,b);
		
		b="require_once('../init.php');";
		writeFile(FileName,b);
		b="require_once('edit_";
		b+=table;
		b+=".php');";
		writeFile(FileName,b);
		b="	$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SELECT * FROM ";
		b+=table;
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="	$result = mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);					
		b="$num=mysql_num_rows($result);";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="echo '<table><tr>";		
		writeFile(FileName,b);	
		for(int i=0;i<field.length;i++){
			b="<td>";
			b+=field[i];
			b+="</td>";			
			writeFile(FileName,b);		
		}

		b="</tr>';";
		writeFile(FileName,b);
		
		
		b="for($i=0;$i<$num;$i++)";
		writeFile(FileName,b);
		b="{";
		writeFile(FileName,b);		
		b="	$id=mysql_result($result,$i,'id');";
		writeFile(FileName,b);
		b="	getData($id);";
		writeFile(FileName,b);			
		b="}";
		writeFile(FileName,b);	
		
		b="echo '</table>';";
		writeFile(FileName,b);		
		b="?>";
		writeFile(FileName,b);
	}	
	
	/*************************************************** edit html *******************************************/
	
	public void editHTML(String FileName)
	{
		String b=new String("");
		b="<form method=\"post\" action=\"edit_";
		b+=table;
		b+="_submit.php\">";
		writeFile(FileName,b);
		b="<table>";
		writeFile(FileName,b);
		
		b="<?";
		writeFile(FileName,b);
		b="$id = $_POST['id'];";
		writeFile(FileName,b);
		
		b="require('init.php');";
		writeFile(FileName,b);
		b="	$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SELECT * FROM ";
		b+=table;
		b+="WHERE id=$id";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="	mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);					
		//b="$num=mysql_numrows($result);";
		//writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		//b="for($i=0;$i<$num;$i++)";
		//writeFile(FileName,b);
		//b="{";
		//writeFile(FileName,b);
		for(int i=0;i<field.length;i++)
		{
			b="	$";
			b+=field[i];
			b+=" = mysql_result($result,$i,\"";
			b+=field[i];
			b+="\");";
			writeFile(FileName,b);
			b="	echo \"<tr>";
			writeFile(FileName,b);
			b="		<td>";			
			b+=field[i];
			b+="</td>";
			writeFile(FileName,b);
			
			b="		<td><input type=\\\"text\\\" name=\\\"";			
			b+=field[i];
			b+="\\\" value=\\\"$";
			b+=field[i];
			b+="\\\"></td>";
			writeFile(FileName,b);
			
			b="	</tr>\";";
			writeFile(FileName,b);
		}		
		//b="}";
		//writeFile(FileName,b);	
		b="?>";
		writeFile(FileName,b);	
		b="	<tr>";
		writeFile(FileName,b);
		b="		<td>";			
		b+="&nbsp ";
		b+="</td>";
		writeFile(FileName,b);
		b="		<td><input type=\"submit\"\\></td>";			
		writeFile(FileName,b);
		b="	</tr>";
		writeFile(FileName,b);	
		b="</table>";
		writeFile(FileName,b);
		b="</form>";
		writeFile(FileName,b);		
	}
	
/******************************************** Edit HTML 2 *****************************/
	
	public void editHTML2(String FileName)
	{				
		String b=new String("");		
		writeFile(FileName,b);		
		b="<?";
		writeFile(FileName,b);
		b="function getData($id){";
		
		writeFile(FileName,b);
		b="require('../init.php');";
		writeFile(FileName,b);
		b="	$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SELECT * FROM ";
		b+=table;
		b+=" WHERE id=$id";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);
		b="";
		writeFile(FileName,b);
		b="	$result = mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);					
		
		b="";
		writeFile(FileName,b);
		b="	echo '<tr><form method=\"post\" action=\"edit_";
		b+=table;
		b+="_submit.php";
		b+="\">';";
		writeFile(FileName,b);
		for(int i=0;i<field.length;i++)
		{
			b="	$";
			b+=field[i];
			b+=" = mysql_result($result,0,\"";
			b+=field[i];
			b+="\");";
			writeFile(FileName,b);						
			b="		echo \"<td><input type=\\\"text\\\" name=\\\"";			
			b+=field[i];
			b+="\\\" value=\\\"$";
			b+=field[i];
			b+="\\\"></td>\";";
			writeFile(FileName,b);			
		}			
		b="	echo '<td>";			
		b+="&nbsp ";
		b+="</td>";
		writeFile(FileName,b);
		b="		<td><input type=\"submit\" value=\"Edit\"></td>";			
		writeFile(FileName,b);
		b="	<input type=\"hidden\" name=\"id\" value=\"'.$id.'\"></td>";					
		writeFile(FileName,b);
		b="		<td><a href=\"del_";
		b+=table;
		b+=".php?id='.$id.'\" >Delete</a></td>";			
		writeFile(FileName,b);
		b="	</td>";
		writeFile(FileName,b);			
		b="</form></tr>';";
		writeFile(FileName,b);		
		b="}";
		writeFile(FileName,b);
		b="?>";
		writeFile(FileName,b);		
	}	
	
/********************************************* Edit submit ***************************/	
	public void editPHP_submit(String FileName)
	{
		String b=new String("");
		b="<?";
		writeFile(FileName,b);
		
		b="require('../init.php');";
		writeFile(FileName,b);
		b="$id = $_POST['id'];";
		writeFile(FileName,b);
		
		for(int i=0;i<field.length;i++)
		{
			b="$";
			b+=field[i];
			b+=" = addslashes($_POST['";
			b+=field[i];
			b+="']);";
			writeFile(FileName,b);
		}
		
		b="			$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="UPDATE ";
		b+=table;
		writeFile(FileName,b);	
		b="SET ";
		for(int i=0;i<field.length;i++)
		{
			b+=field[i];
			b+=" = '$";
			b+=field[i];
			if(i==field.length-1)
				b+="' ";
			else
				b+="' , ";
		}		
		writeFile(FileName,b);	
		b="WHERE id=$id";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);	
		b="	mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);			
		b="echo \"Successfully Edited.\";";
		writeFile(FileName,b);			
		b="?>";
		writeFile(FileName,b);	
	}
/*********************************************** del PHP submit *********************************/
public void delPHP_submit(String FileName)
	{
		String b=new String("");
		b="<?";
		writeFile(FileName,b);
		
		b="require('../init.php');";
		writeFile(FileName,b);
		b="$id = $_GET['id'];";
		writeFile(FileName,b);
		
		b="			$sql = <<<SQL";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="DELETE FROM ";
		b+=table;
		b+=" WHERE id=$id";
		writeFile(FileName,b);	
		b="";
		writeFile(FileName,b);	
		b="SQL;";
		writeFile(FileName,b);	
		b="	mysql_query($sql) or die(mysql_error());";
		writeFile(FileName,b);			
		b="echo \"Successfully Deleted.\";";
		writeFile(FileName,b);			
		b="?>";
		writeFile(FileName,b);	
		
	}	
/********************************************** CRUD ***********************************/	
	
	
	public void CRUD(){
		File x = new File(".");	
		String absPath = x.getAbsolutePath(); 
		new File(absPath+"/"+table).mkdir();
		String workingDir = absPath+"/"+table+"/";
		HtmlForm(workingDir+"add_"+table+".php");
		addPHP(workingDir+"add_"+table+"_submit.php");
		editHTML2(workingDir+"edit_"+table+".php");
		editPHP_submit(workingDir+"edit_"+table+"_submit.php");
		delPHP_submit(workingDir+"del_"+table+".php");
		getPHP2(workingDir+"get_"+table+".php");
		SQL(workingDir+table+".sql");
	}
	
	public void getDataConsole()
	{
		System.out.println("/********************************************/");
		System.out.println("/*                BACodeGenerator            */");
		System.out.println("/*                 Version"+version+"                 */");
		System.out.println("/*             Created by: Atul               */");
		System.out.println("/*       (c) GNU GPL opensource license       */");
		System.out.println("/*   http://code.google.com/p/bacodegenerator */");
		System.out.println("/**********************************************/");
		System.out.println("Table Name:");
		try{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		table=in.readLine();
		String input=new String();		
		Vector v=new Vector();
		do
		{
			input=in.readLine();
			v.addElement(input);			
		}
		while(!input.equals(""));
		
		String[] arr=new String[v.size()-1];
		for(int i=0;i<v.size()-1;i++)
		{
			arr[i]=(String) v.elementAt(i);
		}
		field=arr;
		int code=0;
		do
		{
			System.out.println("1.CRUD 10. Exit");
			
			input = in.readLine();
			code=Integer.parseInt(input);
			/*
			System.out.println("File Name");
			input= in.readLine();
			*/
			switch(code)
			{
				case 1:					
					CRUD();
					//editHTML(input);
				break;
				case 10:
					break;
			}			
		}
		while(code!=10);
		}
		catch(Exception e)
		{
			System.out.println("Error in getDataConsole "+e);
		}
	}
	
	public static void main(String[] str)
	{
		BACodeGenerator a=new BACodeGenerator();
		//a.table=new String("Instr");
		String[] stringArray={"Open","High","Low","Close","Chg","Trade","Volume","Value"};
		//a.field=stringArray;
		a.getDataConsole();		
	}
}

